package com.cec.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cec.mapper.admin.OldsterMapper;
import com.cec.service.admin.OldsterService;
import com.cec.context.BaseContext;
import com.cec.dto.OldsterDTO;
import com.cec.entity.Oldster;
import com.cec.exception.OperationDataException;
import com.cec.result.Result;
import com.cec.result.ResultCode;
import com.cec.vo.OldsterStatusVO;
import com.cec.vo.OldsterVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 老人信息表 服务实现类
 * </p>
 *
 * @author lt
 * @since 2024-10-25
 */
@Slf4j
@Service
public class OldsterServiceImpl extends ServiceImpl<OldsterMapper, Oldster> implements OldsterService {

    @Autowired
    private OldsterMapper oldsterMapper;
    @Override
    public Result pageQueryOldster(OldsterDTO oldsterDTO) {
        Page<Oldster> page = new Page<>(oldsterDTO.getPage(),oldsterDTO.getPageSize());
        QueryWrapper<Oldster> wrapper = new QueryWrapper<>();
        if (oldsterDTO.getUserName()!=null && !"".equals(oldsterDTO.getUserName()))
            wrapper.like("o.user_name",oldsterDTO.getUserName());
        if (oldsterDTO.getPoliticalStatus()!=null)
            wrapper.eq("o.political_status",oldsterDTO.getPoliticalStatus());
        if (oldsterDTO.getSex()!=null)
            wrapper.eq("o.sex",oldsterDTO.getSex());
        wrapper.eq("o.deleted",0)
                .orderByDesc("o.create_time");
        IPage<OldsterVO> oldsterVOIPage = oldsterMapper.queryPageOldster(page,wrapper);
        return Result.success(oldsterVOIPage.getRecords(),oldsterVOIPage.getTotal());
    }

    @Override
    public List<OldsterStatusVO> getOldsterStatus() {
        return oldsterMapper.getOldsterStatus();
    }

    @Override
    public Result saveOldster(OldsterDTO oldsterDTO) {
        log.info("oldsterDTO-->{}",oldsterDTO);
        Oldster oldster = new Oldster();
        BeanUtils.copyProperties(oldsterDTO,oldster);
        oldster.setCreateUser(BaseContext.getCurrentId());
        oldster.setUpdateUser(BaseContext.getCurrentId());
        int i = oldsterMapper.insert(oldster);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public OldsterDTO queryOldsterById(Long id) {
        Oldster oldster = baseMapper.selectById(id);
        OldsterDTO oldsterDTO = new OldsterDTO();
        if (oldster==null)
            throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
        BeanUtils.copyProperties(oldster,oldsterDTO);
        return oldsterDTO;
    }

    @Override
    public void updateOldster(OldsterDTO oldsterDTO) {
        Oldster oldster = new Oldster();
        BeanUtils.copyProperties(oldsterDTO,oldster);
        oldster.setUpdateUser(BaseContext.getCurrentId());
        int i = baseMapper.updateById(oldster);
        if (i <= 0)
            throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result logicDeleteUserById(Integer id) {
        int i = baseMapper.deleteById(id);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result logicBatchDeleteUserByIds(List<Integer> ids) {
        int i = baseMapper.deleteBatchIds(ids);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }


}
